﻿using System.Collections.Generic;
using TeaLight.Core.BL;
using TeaLight.Core.DL;
using TeaLight.DL;
using TeaLight.DL.SQLite;

namespace TeaLight.Core.DAL
{
    public class DataManagerBase<T>
        where T : IBusinessEntity, new()
    {
        public static TableQuery<T> GetTable()
        {
            return Database.GetTable<T>();
        }

        public static IEnumerable<T> GetAll()
        {
            return Database.GetItems<T>();
        }

        public static T GetById(int id)
        {
            return Database.GetItem<T>(id);
        }

        public static T GetById(int? id)
        {
            if (id.HasValue)
                return Database.GetItem<T>(id.Value);
            return default(T);
        }

        public static int Save(T item)
        {
            return Database.SaveItem<T>(item);
        }

        public static void SaveAll(IEnumerable<T> items)
        {
            Database.SaveItems<T>(items);
        }

        public static void UpdateAll(IEnumerable<T> items)
        {
            // todo
            Database.ClearTable<T>();
            Database.SaveItems<T>(items);
        }

        public static int Delete(int id)
        {
            return Database.DeleteItem<T>(id);
        }

        public static void DeleteAll()
        {
            Database.ClearTable<T>();
        }

    }
}
